<template>
  <!--pages/prod-classify/prod-classify.wxml-->

  <view class="container">
    <!-- <view class="line-fix"></view>
    <view class="tit-background"></view> -->
    <view class="prod-list">
      <block v-for="(item, index) in prodList" :key="index">
        <prod :item="item"></prod>
      </block>

      <view v-if="!prodList.length" class="empty">努力加载中，请稍后</view>
    </view>

    <view class="float-view">
      <view class="float-button" @tap="toHomePage">
        <image src="/static/images/tabbar/homepage-sel.png"></image>
      </view>
    </view>

  </view>
  
</template>

<script>
// pages/prod-classify/prod-classify.js
var http = require("../../utils/http.js");
import prod from "../../components/production/production";
//import VConsole from 'vconsole';

//const vConsole = new VConsole();

export default {
  data() {
    return {
      sts: 0,
      prodList: [],
      title: "",
      current: 1,
      size: 10,
      pages: 0,
      tagid: 0
    };
  },

  components: {
    prod
  },
  props: {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	  
  	  console.log("onReachBottom begin");
  	  
    //if (this.current < this.pages) {
      this.setData({
        current: this.current + 1
      });
      this.loadProdData();
    //}
	console.log("onReachBottom end");
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  console.log("onload begin")
    this.setData({
      current: 1,
      pages: 0,
      sts: options.sts,
      title: options.title ? options.title : ""
    });

    if (options.tagid) {
      this.setData({
        tagid: options.tagid
      });
    }

    if (this.sts == 0) {
      if (options.tagid == 1) {
        uni.setNavigationBarTitle({
          title: '每日上新'
        });
      } else if (options.tagid == 2) {
        uni.setNavigationBarTitle({
          title: '商城热卖'
        });
      } else if (options.tagid == 3) {
        uni.setNavigationBarTitle({
          title: '更多宝贝'
        });
      }
    } else if (this.sts == 1) {
      uni.setNavigationBarTitle({
        title: '10-29积分专区'
      });
    } else if (this.sts == 2) {
      uni.setNavigationBarTitle({
        title: '30-49积分专区'
      });
    } else if (this.sts == 3) {
      uni.setNavigationBarTitle({
        title: '50以上积分专区'
      });
    } else if (this.sts == 4) {
      uni.setNavigationBarTitle({
        title: '优惠券活动商品'
      });
    } else if (this.sts == 5) {
      uni.setNavigationBarTitle({
        title: '我的收藏商品'
      });
    } else {
      uni.setNavigationBarTitle({
        title: this.title
      });
    }

    this.loadProdData();
	console.log("onload end")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    /**
     * 跳转到首页
     */
    toHomePage: function () {
      uni.switchTab({
        url: '/pages/index/index'
      });
    },
    /**
     * 加载商品数据
     */
    loadProdData: function () {
		console.log("loadProdDate begin")
      let sts = this.sts;
	  console.log("this.sts=",this.sts)

      if (sts == 0) {
        // 分组标签商品列表
        this.getTagProd();
      } else if (sts == 1) {
        // 新品推荐
        //let url = "/prod/lastedProdPage";
		let url = "/prod/pageByPrice1";
        this.getActProd(url);
      } else if (sts == 2) {
        // 限时特惠
        //let url = "/prod/discountProdList";
		let url = "/prod/pageByPrice2";
        this.getActProd(url);
      } else if (sts == 3) {
        // 每日疯抢
        //let url = "/prod/moreBuyProdList";
		let url = "/prod/pageByPrice3";
        this.getActProd(url);
      } else if (sts == 4) {
        // 优惠券商品列表
        this.getProdByCouponId(options.tagid);
      } else if (sts == 5) {
        // 收藏商品列表
        this.getCollectionProd();
      }
	  console.log("loadProdDate end")
    },
    getActProd: function (url) {
		 let _thisGetActProd = this;
		console.log("getActProd begin",url,_thisGetActProd.current,_thisGetActProd.size)
     
      uni.showLoading();
      var params = {
        url: url,
        method: "GET",
        data: {
          current: _thisGetActProd.current,
          size: _thisGetActProd.size
        },
        callBack: function (res) {
			console.log("getActProd res",res)
          let list = [];

          if (res.current == 1) {
            list = res.records;
          } else {
            list = _thisGetActProd.prodList;
            list = list.concat(res.records);
          }

          _thisGetActProd.setData({
            prodList: list,
            pages: res.pages
          });
          uni.hideLoading();
		  console.log("getActProd end",url)
        }
      };
      http.request(params);
    },

    /**
     * 获取我的收藏商品
     */
    getCollectionProd: function () {
      let _thisGetCollectionProd = this;
      uni.showLoading();
      var params = {
        url: "/p/user/collection/prods",
        method: "GET",
        data: {
          current: _thisGetCollectionProd.current,
          size: _thisGetCollectionProd.size
        },
        callBack: function (res) {
          let list = [];

          if (res.current == 1) {
            list = res.records;
          } else {
            list = _thisGetCollectionProd.prodList;
            list = list.concat(res.records);
          }

          _thisGetCollectionProd.setData({
            prodList: list,
            pages: res.pages
          });
          uni.hideLoading();
        }
      };
      http.request(params);
    },

    /**
     * 获取标签列表
     */
    getTagProd: function () {
      let _thisGetTagProd  = this;
      uni.showLoading();
      var param = {
        url: "/prod/prodListByTagId",
        method: "GET",
        data: {
          tagId: _thisGetTagProd.tagid,
          current: _thisGetTagProd.current,
          size: _thisGetTagProd.size
        },
        callBack: res => {
          let list = [];

          if (res.current == 1) {
            list = res.records;
          } else {
            list = _thisGetTagProd.prodList.concat(res.records);
          }

          _thisGetTagProd.setData({
            prodList: list,
            pages: res.pages
          });
          uni.hideLoading();
        }
      };
      http.request(param);
    },

    /**
     * 获取优惠券商品列表
     */
    getProdByCouponId(id) {
      let _thisGetProdByCouponId = this;
      uni.showLoading();
      var param = {
        url: "/coupon/prodListByCouponId",
        method: "GET",
        data: {
          couponId: id,
          current: _thisGetProdByCouponId.current,
          size: _thisGetProdByCouponId.size
        },
        callBack: res => {
          let list = [];

          if (res.current == 1) {
            list = res.records;
          } else {
            list = _thisGetProdByCouponId.prodList.concat(res.records);
          }

          _thisGetProdByCouponId.setData({
            prodList: list,
            pages: res.pages
          });
          uni.hideLoading();
        }
      };
      http.request(param);
    }

  }
};
</script>
<style>
@import "./prod-classify.css";
</style>
